<template>
<!-- 组织架构 -->
  <div class="department-container">
    <div class="app-container">
      <el-card>
          <!-- 用一个行列布局 -->
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>


           <el-tree :data="list" >
                <template #default="{data}">
                    <!-- 使用el-tree方主体内容 -->
                <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{data.name}}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{data.manager}}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hDel(data.id)" v-if="data.children.length===0">删除子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>          
                </template>

                
         </el-tree>
      </el-card>   

      <el-dialog  :title="isEdit?'编辑子部门':'新增子部门'" :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="resetFrom"
      >
      <!-- 子组件 -->
      <!-- 解决数据不更新问题 v-if -->
        <addOrEdit  @close="showDialog=false" :origin-list="originList" ref="addOrEditRef" :id="curId" @success="hSuccess" :isEdit="isEdit"></addOrEdit>
      </el-dialog>
    </div>
  </div>
</template>



<script>
import { getDepartments ,delDepartmentDetail } from "@/api/departments";
import { tranDataToTree } from "@/utils";
import addOrEdit from "./deptDialog.vue";
// console.log(tranDataToTree);
  export default {
    components:{addOrEdit},
    data() {
      return {
        showDialog:false,
       // 依赖一份树形数据
      list: [],  
      curId:'', 
      // 编辑
      isEdit:false, 
      originList:[]
      };
    },

    created(){
      this.loadDepartments()
    },
    // 点击获取id传入子组件
    methods: {
      // 清空表单
      resetFrom(){
        this.$refs.addOrEditRef.resetFrom()
      },
      // 删除
      hDel(id){
         this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
        await delDepartmentDetail(id)
        this.loadDepartments()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      hSuccess(){
        this.loadDepartments()
        this.showDialog=false
      },
      // 修改
      hEdit(id){
        this.curId=id
        this.isEdit=true
        this.showDialog=true
        // 解决方法二  this.$refs.addOrEditRef 可以获取子组件 可以直接操作子组件里面的一切
        // this.$nextTick()等待dom更新完成的回调函数 才会执行回调函数中的代码

         this.$nextTick(()=>{
          this.$refs.addOrEditRef.loadDepartmentDetail()
          // console.log( this.$refs.addOrEditRef);
         });
      },
      // 添加
      hAdd(id){
        this.showDialog=true
        this.isEdit=false
        this.curId =id 
      },

  async  loadDepartments(){
    try {
      const res =  await getDepartments()
    // console.log(res);
    res.data.depts.shift()
    this.originList=res.data.depts.map((item)=>{
      return {id:item.id,pid:item.pid,code:item.code,name:item.name}
    })
    this.list = tranDataToTree(res.data.depts)
    // this.list = res.data.depts
    } catch (error) {
      console.log(error);
    }
    }
     
    
    }
  };
</script>
